package com.space.jwt.api;

import com.baomidou.mybatisplus.plugins.Page;

import com.space.jwt.Dto.PageDto;
import com.space.jwt.common.util.MapRemoveNullUtil;
import com.space.jwt.common.util.QueryUtils;
import com.space.jwt.common.util.ServerResponse;
import com.space.jwt.entities.SysUser;
import com.space.jwt.service.ISysUserService;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @标题：用户信息表 controller层
 * @描述：
 * @包名：com.space.jwt.controller
 * @项目名：springboot-jwt
 * @公司：
 * @创建者：crit_space
 * @修改者：crit_space
 * @创建日期： 2019-03-06
 * @修改日期： 2019-03-06
 * @版本：1.0
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/sysUser")
public class SysUserController {
    @Autowired
    ISysUserService iSysUserService;

                                                                                                                                                                                                                                /**
     * 查看信息
     */

    @ApiOperation(value = "查看详细信息")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
    @GetMapping("/info/{id}")
    public ServerResponse info(@PathVariable("id") String id){
        SysUser sysUser = iSysUserService.selectById(id);
            return ServerResponse.successData(sysUser);
            }
    /**
    * 保存
    */
    @ApiOperation(value = "新增保存操作")
    @PostMapping("/save")
    public ServerResponse save(@RequestBody  SysUser sysUser){
        boolean flag = iSysUserService.insert(sysUser);
            return ServerResponse.trueOrNo(flag,null);
    }

    /**
    * 修改
    */
    @ApiOperation(value = "修改保存操作")
    @PutMapping("/update")
    public ServerResponse update(@RequestBody SysUser sysUser){
        boolean flag = iSysUserService.updateById(sysUser);
        return ServerResponse.trueOrNo(flag,null);
    }

    /**
    * 删除
    */
    @ApiOperation(value = "删除操作")
    @DeleteMapping("/delete")
    public ServerResponse delete(@RequestBody String id){
        boolean flag = iSysUserService.deleteById(id);
        return ServerResponse.trueOrNo(flag,null);
    }
    /**
    * 根据id批量删除操作
    */
    @ApiOperation(value = "根据id批量删除操作,注:多个id间用,号隔开")
    @DeleteMapping("/batchDelete")
    public ServerResponse deleteByIds(@RequestBody String ids){
        List<String> ids1 = Arrays.asList(ids.split(","));
        boolean flag = iSysUserService.deleteBatchIds(ids1);
        return ServerResponse.trueOrNo(flag,null);
    }
    /**
    * 查看所有列表
    */
    @ApiOperation(value = "根据条件查询列表操作")
    @PostMapping("/queryAll")
    public ServerResponse queryAll(@RequestBody Map<String, Object> params){
        //移除空值
        MapRemoveNullUtil.removeNullEntry(params);
        Map<String, Object> map = QueryUtils.getRealMap(new SysUser(), params);
        List<SysUser> list=  iSysUserService.selectByMap(map);
        return ServerResponse.successData(list);
    }

    /**
    * 无条件分页列表查询操作
    */
    @ApiOperation(value = "无条件分页查询操作")
    @PostMapping("/queryPageList")
    public ServerResponse queryAll(@RequestBody PageDto pageDto){
        HashMap<String ,Object> map = pageDto.getMap();
        Page<SysUser>  page = new Page(pageDto.getCurrent(), pageDto.getSize());
        page.setCondition(map);
        iSysUserService.selectPageList(page);
        return ServerResponse.successData(page);
    }
}
